$package('YiYa.merchant');
YiYa.merchant = function(){
	var _box = null;
	var _this = {
		config:{
			event:{
				edit:function(){
					var selected = _box.utils.getCheckedRows();
					if ( _box.utils.checkSelectOne(selected)){
						var record = selected[0];
						_box.handler.edit();
						var url = '../product/loadProdListByMerchant.do?merchantId='+record.merchant.id;
						$.get(url,function(data){
							$('#cc3').combogrid('grid').datagrid('loadData',data);
				        });
						$('#cc1').combobox('setValue',record.merchant.type);
					}
				},
				view:function(){
					var selected = _box.utils.getCheckedRows();
					if ( _box.utils.checkSelectOne(selected)){
						var record = selected[0];
						_box.handler.view();
						var url = '../merchant/loadMerchantList.do?type='+record.merchant.type;
						$('#cc5').combobox('reload',url);
						$('#cc4').combobox('setValue',record.merchant.type);
						var url2 = '../product/loadProdListByMerchant.do?merchantId='+record.merchant.id;
						$.get(url2,function(data){
							$('#cc6').combogrid('grid').datagrid('loadData',data);
				        });
					}
				}
			},
			dataGrid:{
				title:'历史报价列表',
				url:'dataList.do',
				columns:[[
				          {field:'id',checkbox:true},
				          {field:'productName',title:'产品名称',width:120},
						  {field:'productColor',title:'产品颜色',width:80},
						  {field:'productStandard',title:'产品规格',width:80},
						  {field:'pay_type',title:'付款方式',width:80,sortable:true,
				        	  formatter:function(value,row,index){
				        		  return row["payType"];
						  }},
						  {field:'merchant',title:'商家',width:80,
				        	  formatter:function(value,row,index){
									var name = '';
									if(value!=null){
										name = value.name;
									}
									return name;
						  }},
						  {field:'merchant_type',title:'商家类型',width:80,
				        	  formatter:function(value,row,index){
				        		  var result = '';
				        		  if(new Object(row["merchant"]).type==1){
				        			  result = '供应商';
				        		  }else if(new Object(row["merchant"]).type==2){
				        			  result = '客户';
				        		  }
				        		  return result;
						  }},
				          {field:'price',title:'价格',width:80,sortable:true},
				          {field:'ph.change_time',title:'报价时间',width:120,sortable:true,
				        	  formatter:function(value,row,index){
				        		  return new Date(row["changeTime"].split(" ")[0].replace(/-/g,'/')).format("yyyy-MM-dd");
						  }},
						  {field:'monthly_balance_date',title:'月结日期',width:120,sortable:true,
				        	  formatter:function(value,row,index){
				        		  if(row["monthlyBalanceDate"]!=null){
				        		  return new Date(row["monthlyBalanceDate"].split(" ")[0].replace(/-/g,'/')).format("yyyy-MM-dd");
				        		  }
						  }},
						  {field:'tax',title:'是否含税',width:60,sortable:true,
				        	  formatter:function(value,row,index){
					        		if(value == 0 || value == null){
					        			return "否";
					        		}
					        		if(value == 1){
					        			return "是";
					        		}
					        	}  
				          },
				          {field:'discount',title:'是否扣%',width:60,sortable:true,
				        	  formatter:function(value,row,index){
					        		if(value == 0 || value == null){
					        			return "否";
					        		}
					        		if(value == 1){
					        			return "是";
					        		}
					        	}  
				          }
				          ]],
				          toolbar:[
				                   {id:'btnadd',text:'添加',btnType:'add'},
				                   {id:'btnedit',text:'编辑',btnType:'edit'},
				                   {id:'btndelete',text:'删除',btnType:'remove'},
				                   {id:'btnview',text:'查看',btnType:'view'}
				                   ]
			}
		},
		init:function(){
			/*
			$('.easyui-datebox').datebox({
			    formatter: function(date){ 
			    	if(date!=null){
			    		return date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
			    	}
		    	},
			    parser: function(date){ 
			    	if(date!=null){
			    		return new Date(Date.parse(date.replace(/-/g,"/")));
			    	}
		    	}
			});
			*/
			$('#cc1').combobox({
			    onChange:function(newValue,oldValue){
			    	url = '../merchant/loadMerchantList.do?type='+newValue;
			    	$('#cc2').combobox('reload',url);
			    	$("#cc2").combobox('clear');
			    	$("#cc3").combogrid('clear');
			    }
			});
			_box = new YDataGrid(_this.config);
			_box.init();
		}
	}
	return _this;
}();

$(function(){
	YiYa.merchant.init();
});


